<template>
    <div class="debris-list" v-loading="loading">
        <div class="input-box">
            <span class="input-title" style="margin-left:0px;">用户昵称</span>
            <el-input
                placeholder="搜索用户昵称"
                @keyup.enter.native="selectChange"
                style="width:150px;"
                clearable
                @clear="clearInput"
                v-model.trim="paramsList.userName">
            </el-input>
            <span class="input-title" style="margin-right: 24px;">用户ID</span>
            <el-input
                placeholder="搜索用户ID"
                @keyup.enter.native="selectChange"
                style="width:130px;"
                clearable
                @clear="clearInput"
                v-model.trim="paramsList.userId">
            </el-input>
            <span class="input-title">权限</span>
            <el-select @change="getList(1)" multiple collapse-tags clearable style="width:170px;" v-model="paramsList.identity">
                <el-option label="队员" :value="0"></el-option>
                <el-option label="队长" :value="4"></el-option>
                <el-option label="运营" :value="5"></el-option>
            </el-select>
            <span class="input-title">来源</span>
            <el-select @change="getList(1)" multiple clearable collapse-tags style="width:200px;" v-model="paramsList.channel">
                <el-option v-for="(item, index) in fromList" :key="index" :label="item.desc" :value="item.channel"></el-option>
            </el-select>
        </div>
        <div class="input-box">
            <span class="input-title" style="margin-left:0px;">队长等级</span>
            <el-select @change="getList(1)" multiple clearable collapse-tags style="width:150px;" v-model="paramsList.teamCaptainLevel">
                <el-option label="见习" :value="1"></el-option>
                <el-option label="铜牌" :value="2"></el-option>
                <el-option label="银牌" :value="3"></el-option>
                <el-option label="金牌" :value="4"></el-option>
                <el-option label="钻石" :value="5"></el-option>
                <el-option label="荣耀" :value="6"></el-option>
            </el-select>
            <span class="input-title">队长补贴</span>
            <el-select @change="getList(1)" style="width:130px;" v-model="paramsList.openReward">
                <el-option label="全部" :value="null"></el-option>
                <el-option label="开启" :value="0"></el-option>
                <el-option label="关闭" :value="1"></el-option>
            </el-select>
            <span class="input-title">区服</span>
            <el-select @change="getList(1)" style="width:170px;" v-model="paramsList.serverType">
                <el-option label="全部" :value="null"></el-option>
                <el-option label="微信" :value="1"></el-option>
                <el-option label="QQ" :value="2"></el-option>
            </el-select>
            <span class="input-title">段位</span>
            <el-select @change="getList(1)" multiple collapse-tags clearable style="width:200px;" v-model="paramsList.jobLevel">
                <el-option label="钻石" :value="14"></el-option>
                <el-option label="星耀" :value="15"></el-option>
                <el-option label="王者0-25星" :value="16"></el-option>
                <el-option label="王者26-50星" :value="17"></el-option>
                <el-option label="王者51-75星" :value="18"></el-option>
                <el-option label="王者76-100星" :value="19"></el-option>
                <el-option label="王者101-125星" :value="20"></el-option>
                <el-option label="王者126-150星" :value="21"></el-option>
            </el-select>
        </div>
        <div class="input-box">
            <span class="input-title" style="margin-left:0px;">注册时间</span>
            <el-date-picker
                @change="changeDate"
                v-model="date"
                :clearable="true"
                type="daterange"
                value-format="yyyy-MM-dd"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期">
            </el-date-picker>
            <el-button type="primary" class="btn" style="margin-left:20px" @click="getList(1)">查询</el-button>
        </div>
        <el-divider></el-divider>
        <div class="table-box">
            <div class="table-tools">
                <el-button type="text" size="mini" class="btn" @click="changeIde(4, true)">设为队长</el-button>
                <el-button type="text" size="mini" class="btn" style="margin-left:20px" @click="changeIde(0, true)">设为队员</el-button>
                <el-button type="text" size="mini" class="btn" style="margin-left:20px" @click="changeIde(5, true)">设为运营</el-button>
                <el-button type="text" size="mini" class="btn" style="margin-left:20px" @click="changeIde(1)">开启补贴</el-button>
                <el-button type="text" size="mini" class="btn" style="margin-left:20px" @click="changeIde(0)">关闭补贴</el-button>
            </div>
            <el-table :data="tableData" ref="table" @sort-change="sortChange" border style="width: 100%" @selection-change="selectionChange">
                <el-table-column fixed="left" type="selection" width="40"></el-table-column>
                <el-table-column min-width="240" prop="gameName" label="用户">
                    <template slot-scope="scope">
                        <div class="table-flex">
                            <img class="user-avatar" :src="scope.row.userIcon" alt="">
                            <div class="user-info">
                                <p class="table-btn" @click="goUserInfo(scope.row.userId )">趣ID：{{ scope.row.userId }}</p>
                                <p>昵称：{{ scope.row.nickName }}</p>
                            </div>
                        </div>
                    </template>
                </el-table-column>
                <el-table-column min-width="150" label="权限">
                    <template slot-scope="scope">
                        <span v-if="scope.row.identity === 0">队员</span>
                        <span v-if="scope.row.identity === 4">队长</span>
                        <span v-if="scope.row.identity === 5">运营</span>
                    </template>
                </el-table-column>
                <el-table-column min-width="80" label="区服">
                    <template slot-scope="scope">
                        <span v-if="scope.row.serverType === 1">微信</span>
                        <span v-if="scope.row.serverType === 2">QQ</span>
                    </template>
                </el-table-column>
                <el-table-column min-width="120" label="段位">
                    <template slot-scope="scope">
                        <p v-for="(item, index) in scope.row.captainJobLevels" :key="index">
                            <span v-if="item === 14">钻石 </span>
                            <span v-if="item === 15">星耀 </span>
                            <span v-if="item === 16">王者0-25星 </span>
                            <span v-if="item === 17">王者26-50星 </span>
                            <span v-if="item === 18">王者51-75星 </span>
                            <span v-if="item === 19">王者76-100星 </span>
                            <span v-if="item === 20">王者101-125星 </span>
                            <span v-if="item === 21">王者126-150星 </span>
                        </p>
                    </template>
                </el-table-column>
                <el-table-column min-width="120" label="等级" prop="level" sortable="custom"></el-table-column>
                <el-table-column min-width="150" label="星级" prop="score" sortable="custom"></el-table-column>
                <el-table-column min-width="100" label="粉丝" prop="fansNum" sortable="custom"></el-table-column>
                <el-table-column min-width="160" label="累计收益(趣金币)" prop="income" sortable="custom">
                    <template slot-scope="scope">
                        <div v-if="scope.row.income">{{ scope.row.income }}</div>
                        <div v-else>0</div>
                    </template>
                </el-table-column>
                <el-table-column min-width="120" label="补贴">
                    <template slot-scope="scope">
                        <div v-if="scope.row.openReward === 1">开启</div>
                        <div v-else>关闭</div>
                    </template>
                </el-table-column>
                <el-table-column min-width="100" label="下载来源">
                    <template slot-scope="scope">
                        <div v-for="(item, index) in fromList" :key="index">
                            <span v-if="scope.row.channel === item.channel">{{ item.desc }}</span>
                        </div>
                        <span v-if="!scope.row.channel">-</span>
                    </template>
                </el-table-column>
                <el-table-column width="170" label="注册时间">
                    <template slot-scope="scope">
                        <div>{{ tableFormatDate(scope.row.createTime) }}</div>
                    </template>
                </el-table-column>
                <el-table-column fixed="right" label="操作">
                    <template slot-scope="scope">
                        <el-button @click="goUserDetail(scope.row.userId)" type="text" size="small">查看详情</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </div>

        <div class="tools">
            <el-pagination
                @size-change="sizeChange"
                @current-change="pageChange"
                :current-page="pageNum"
                :page-sizes="_pageSizes"
                :page-size="pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total">
            </el-pagination>
        </div>
    </div>
</template>

<script>
import { formatDate } from '@/utils/utils'

export default {
    name: 'userList',
    data () {
        return {
            keyWord: '',
            sort: '',
            currentSortField: '',
            status: null,
            paramsList: {
                fromDate: null,
                toDate: null
            },
            date: [],
            fromList: [],
            tableData: [],
            pageNum: 1,
            pageSize: this._pageSize,
            total: 0,
            loading: false
        }
    },
    created () {
        this.getList(1)
        this.getFromList()
    },
    methods: {
        // 获取列表
        async getList (pageNum) {
            if (this.loading) return
            this.pageNum = pageNum
            this.loading = true
            let params = {
                pageNum,
                pageSize: this.pageSize,
                currentSortField: this.currentSortField || null,
                sort: this.sort || null
            }
            params = Object.assign(this.paramsList, params)

            let res = await this.$http.post('/team/getTeamUserInfo', params)
            if (res.code === '0') {
                this.tableData = res.data
                this.total = res.count
            } else {
                this.$message({
                    type: 'error',
                    message: '系统异常，请稍后再试'
                })
            }
            this.loading = false
        },

        // 页码切换
        sizeChange (val) {
            this.pageNum = 1
            this.pageSize = val
            this.getList(1)
        },
        // 页容切换
        pageChange (val) {
            this.pageNum = val
            this.getList(this.pageNum)
        },

        goUserInfo (id) {
            const { href } = this.$router.resolve({
                path: '/dataReport/userInfo',
                query: {
                    id,
                    tab: 'quTeam'
                }
            })
            window.open(href, '_blank')
        },
        async getFromList () {
            let res = await this.$http.get('/apkManage/getChannel')
            if (res.code === '0') {
                this.fromList = res.data
            } else {
                this.$message({
                    type: 'error',
                    message: res.desc
                })
            }
        },
        goUserDetail (userId) {
            const { href } = this.$router.resolve({
                path: '/team/teamUser/userDetail',
                query: {
                    userId
                }
            })
            window.open(href, '_blank')
        },
        changeDate (val) {
            if (val) {
                this.paramsList.fromDate = `${val[0]} 00:00:00`
                this.paramsList.toDate = `${val[1]} 23:59:59`
            } else {
                this.paramsList.fromDate = null
                this.paramsList.toDate = null
            }
            this.getList(1)
        },
        changeIde (type, isIde) {
            if (!this.userIdList || this.userIdList.length === 0) {
                return this.$message({
                    type: 'warning',
                    message: '请选择修改的用户'
                })
            }
            let url = '/team/updateOpenReward'
            if (isIde) url = '/team/updateIdentity'
            this.$http.post(url, {
                userIdList: this.userIdList,
                type
            }).then(res => {
                if (res.code === '0') {
                    this.userIdList.map(userItem => {
                        this.tableData.find(tabItem => {
                            if (tabItem.userId === userItem) {
                                if (isIde) {
                                    tabItem.identity = type
                                } else {
                                    tabItem.openReward = type
                                }
                            }
                        })
                    })
                    this.$message({
                        type: 'success',
                        message: '权限修改成功'
                    })
                } else {
                    this.$message({
                        type: 'error',
                        message: '系统异常，请稍后再试'
                    })
                }
            })
        },
        selectionChange (val) {
            this.userIdList = []
            if (val.length > 0) {
                val.map(item => {
                    this.userIdList.push(item.userId)
                })
            }
        },
        selectChange () {
            this.type = ''
            this.sort = ''
            this.$refs.table.clearSort()
            this.getList(1)
        },
        sortChange ({ column, prop, order }) {
            this.sort = order || ''
            // 如果没有 sort prop 置为空
            if (!this.sort) {
                this.type = null
            } else {
                if (this.sort === 'ascending') {
                    this.sort = 'asc'
                } else {
                    this.sort = 'desc'
                }
                this.currentSortField = prop
            }
            this.getList(1)
        },
        clearInput () {
            this.getList(1)
        },
        tableFormatDate (time) {
            return formatDate(time, 'YYYY-MM-DD HH:MM:SS')
        }
    }
}
</script>

<style lang="scss" scoped>
    .debris-list {
        .input-box {
            margin-top: 10px;
            font-size: 13px;
            font-weight: 500;
            .input-title {
                margin: 0 10px 0 20px;
            }
            .btn {
                margin-top: 10px;
                margin-left: 10px;
            }
        }

        .table-box {
            margin: 20px 0;
            .table-tools {
                margin-bottom: 10px;
            }
            .cover {
                width: 80px;
                height: 80px;
                > img {
                    width: 100%;
                    height: 100%;
                }
            }
            .user-icon {
                width: 20px;
                height: 20px;
                border-radius: 50%;
                margin-right: 5px;
                vertical-align: top;
            }
            .table-flex {
                display: flex;
                align-items: center;
                .user-avatar {
                    display: inline-block;
                    min-width: 60px;
                    height: 60px;
                    margin-right: 5px;
                    border-radius: 50%;
                }
                .user-info {
                    display: flex;
                    flex-direction: column;
                    justify-content: center;
                }
            }
            .table-btn {
                cursor: pointer;
                color: #409EFF;
                font-size: 13px;
                font-weight: 500;
                &:hover {
                    text-decoration: underline;
                }
            }
        }

        .tools {
            padding-right: 20px;
            display: flex;
            justify-content: flex-end;
        }
    }
</style>
